import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: {
      icon: 'el-icon-s-home', // 首页图标
      title: '仪表盘',
      roles: ['admin'] // 可根据角色控制权限
    },
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '仪表盘', affix: true }
      }
    ]
  },
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/index',
    meta: {
      icon: 'el-icon-goods', // 商品图标
      title: '商品管理',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/goods/index'),
        name: 'Goods',
        meta: { title: '商品管理' }
      }
    ]
  },
  {
    path: '/inventory',
    component: Layout,
    redirect: '/inventory/index',
    meta: {
      icon: 'el-icon-takeaway-box', // 库存图标
      title: '库存管理',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/inventory/index'),
        name: 'Inventory',
        meta: { title: '库存管理' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    redirect: '/order/index',
    meta: {
      icon: 'el-icon-document', // 订单图标
      title: '订单管理',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/order/index'),
        name: 'Order',
        meta: { title: '订单管理' }
      }
    ]
  },
  {
    path: '/machine',
    component: Layout,
    redirect: '/machine/index',
    meta: {
      icon: 'el-icon-monitor', // 设备图标
      title: '设备管理',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/machine/index'),
        name: 'Machine',
        meta: { title: '设备管理' }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    meta: {
      icon: 'el-icon-user', // 用户图标
      title: '用户中心',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/user/index'),
        name: 'UserCenter',
        meta: { title: '用户管理' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
